#####################################################################################
####Name of File: ifb_comparison_statevnatl.R
####Input dataset: "Unrestricted_SWU.Rdata"
####Output : state specific regression output
####Date: 4/24/2011
####Purpose: regress ifb on jsy and covariates for each state and create ROC plots
#####################################################################################

###NOTE: MUST FIRST RUN THE FOLLOWING FILES TO MAKE THIS WORK:
##"National_outcome_reg.R" and "ifb_statespecific.R"

###take national model betas- ifb.logit

summary(ifb.logit)


###do state for uttar pradesh
DF1<-as.data.frame(all.data2[[32,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


ifb.logit.UP<-zelig(ifb~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)

summary(ifb.logit1)



####do state for Rajasthan

DF1<-as.data.frame(all.data2[[28,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


ifb.logit.raj<-zelig(ifb~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)

summary(ifb.logit1)


####do state for Madhya Pradesh

DF1<-as.data.frame(all.data2[[20,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


ifb.logit.MP<-zelig(ifb~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)



####do state for Bihar

DF1<-as.data.frame(all.data2[[5,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


ifb.logit.Bihar<-zelig(ifb~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)

####do state for Sikkim

DF1<-as.data.frame(all.data2[[29,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


ifb.logit.Sikkim<-zelig(ifb~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)




####do state for Karnataka

DF1<-as.data.frame(all.data2[[17,1]])

DF1$nb_cat<-as.factor(DF1$nb_cat_num)
DF1$mateduc_cat<-as.factor(DF1$mateduc_cat)
DF1$dlhs3_country_decile<-as.factor(DF1$dlhs3_country_decile)
DF1$caste1<-as.factor(DF1$caste)
DF1$state1<-as.factor(DF1$state)
DF1$dist.char<-as.character(DF1$DIST_mean_hh_p_income3)
DF1$district<-as.factor(DF1$dist.char)
DF1$religion2<-as.factor(DF1$religion.char)

print(unique(DF1$state1))


ifb.logit.Karnataka<-zelig(ifb~jsy+matage_cat+nb_cat+birth_interval+mult_birth+mateduc_cat+dlhs3_country_decile+caste1+ religion2+res_cat+ 
+district, model="logit", weights="weights", data=DF1, save.data=T)






#######################################ROC curves

par(mfrow=c(3,2))



#########Bihar



###fitted values
Bihar.fitted.natl<-fitted(ifb.logit)[full.data$state=="Bihar"]
Bihar.fitted.state<-fitted(ifb.logit.Bihar)

length(Bihar.fitted.natl)
length(fitted(ifb.logit.Bihar))

###y values
Bihar.response.natl<-ifb.logit$y[full.data$state=="Bihar"]
length(Bihar.response.natl)

Bihar.response.state<-ifb.logit.Bihar$y
length(Bihar.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(Bihar.response.state ,Bihar.response.natl,  Bihar.fitted.state,Bihar.fitted.natl,  main="Bihar (High Focus)")




##############Uttar Pradesh
###fitted values
UP.fitted.natl<-fitted(ifb.logit)[full.data$state=="Uttar Pradesh"]
UP.fitted.state<-fitted(ifb.logit.UP)

length(UP.fitted.natl)
length(UP.fitted.state)

###y values
UP.response.natl<-ifb.logit$y[full.data$state=="Uttar Pradesh"]
length(UP.response.natl)

UP.response.state<-ifb.logit.UP$y
length(UP.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(UP.response.state , UP.response.natl, UP.fitted.state,UP.fitted.natl,main="Uttar Pradesh (High Focus)")





#########Madhya Pradesh



###fitted values
MP.fitted.natl<-fitted(ifb.logit)[full.data$state=="Madhya Pradesh"]
MP.fitted.state<-fitted(ifb.logit.MP)

length(MP.fitted.natl)
length(fitted(ifb.logit.MP))

###y values
MP.response.natl<-ifb.logit$y[full.data$state=="Madhya Pradesh"]
length(MP.response.natl)

MP.response.state<-ifb.logit.MP$y
length(MP.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(MP.response.state ,MP.response.natl,  MP.fitted.state,MP.fitted.natl,  main="Madhya Pradesh (High Focus)")





#########Karnataka



###fitted values
Karnataka.fitted.natl<-fitted(ifb.logit)[full.data$state=="Karnataka"]
Karnataka.fitted.state<-fitted(ifb.logit.Karnataka)

length(Karnataka.fitted.natl)
length(fitted(ifb.logit.Karnataka))

###y values
Karnataka.response.natl<-ifb.logit$y[full.data$state=="Karnataka"]
length(Karnataka.response.natl)

Karnataka.response.state<-ifb.logit.Karnataka$y
length(Karnataka.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(Karnataka.response.state ,Karnataka.response.natl,  Karnataka.fitted.state,Karnataka.fitted.natl,  main="Karnataka")


##########Rajasthan


###fitted values
Raj.fitted.natl<-fitted(ifb.logit)[full.data$state=="Rajasthan"]
Raj.fitted.state<-fitted(ifb.logit.raj)

length(Raj.fitted.natl)
length(fitted(ifb.logit.raj))

###y values
Raj.response.natl<-ifb.logit$y[full.data$state=="Rajasthan"]
length(Raj.response.natl)

Raj.response.state<-ifb.logit.raj$y
length(Raj.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(Raj.response.state ,Raj.response.natl,Raj.fitted.state, Raj.fitted.natl, main="Rajasthan (High Focus)")


#########Sikkim


###fitted values
Sikkim.fitted.natl<-fitted(ifb.logit)[full.data$state=="Sikkim"]
Sikkim.fitted.state<-fitted(ifb.logit.Sikkim)

length(Sikkim.fitted.natl)
length(fitted(ifb.logit.Sikkim))

###y values
Sikkim.response.natl<-ifb.logit$y[full.data$state=="Sikkim"]
length(Sikkim.response.natl)

Sikkim.response.state<-ifb.logit.Sikkim$y
length(Sikkim.response.state)

###ROC plot comparing state specific for state of Uttar Pradesh
rocplot(Sikkim.response.state ,Sikkim.response.natl,  Sikkim.fitted.state,Sikkim.fitted.natl,  main="Sikkim")





















